﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lswweb.Model;
using Lswweb.IDAL;
using System.Web.UI.WebControls;

namespace Lswweb.Domain
{
    public class Links
    {
        private static ILink linkDAL = DALFactory.CreateLink();

        public static LinkInfo GetLink(int Id)
        {
            if (Id <= 0)
                Id = SiteContext.Current.Id;

            if (Id <= 0)
                return null;

            return linkDAL.GetLink(Id);
        }
        public static LinkInfo GetLink(string linkUrl)
        {
            if (string.IsNullOrEmpty(linkUrl))
                return null;

            return linkDAL.GetLink(linkUrl);
        }

        public static IList<LinkInfo> GetLinks(string searchKey,int itemId)
        {
            return linkDAL.GetLinks(searchKey, itemId);
        }

        public static void Insert(LinkInfo info)
        {
            info = FileHandler(info);
            if (linkDAL.Insert(info) > 0)
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void Update(LinkInfo info)
        {
            if (info == null || info.Id<=0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            LinkInfo oldInfo = linkDAL.GetLink(info.Id);
            if (!string.IsNullOrEmpty(info.ImageUrl) && info.ImageUrl != oldInfo.ImageUrl)
                Globals.FileDelete(oldInfo.ImageUrl);

            info = FileHandler(info);            

            if (linkDAL.Update(info))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }


        public static void Delete(int id)
        {
            string returnUrl = SiteContext.Current.ReferrerUrl;

            LinkInfo oldLink = linkDAL.GetLink(id);
            if (oldLink ==null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError, returnUrl);
                return;
            }

            if (!string.IsNullOrEmpty(oldLink.ImageUrl))
                Globals.FileDelete(oldLink.ImageUrl);

            if (linkDAL.Delete(id))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, returnUrl);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError, returnUrl);
        }

        private static LinkInfo FileHandler(LinkInfo info)
        {
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return null;
            }

            if (info.Id <= 0 && SiteContext.Current.Id > 0)
                info.Id = SiteContext.Current.Id;

            info.ImageUrl = ImageHelper.MoveImagesToDirectory(SiteModule.Links, info.ImageUrl);

            if (string.IsNullOrEmpty(info.Ver))
                info.Ver = SiteContext.Current.Language;

            return info;
        }
    }
}
